﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.BackModels.Models.Right;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using Wwtfly.DBUtility;

namespace Wwtfly.BBS.BackDAL.Right
{
  public partial class UAUserOfRoleDAL
    {
        public const string UAUSEROFROLE_SELECT_BY_ID = " SELECT [UserID],[RoleID],[Status],[CreateUser],[CreateTime],[LastModifyUser],[LastModifyTime]" +
                                            " FROM [UAUserOfRole]" +
                                            " WHERE (" +
                                            " [UserID] = @UserID)";

        public const string UAUSEROFROLE_SELECT = " SELECT [UserID],[RoleID],[Status],[CreateUser],[CreateTime],[LastModifyUser],[LastModifyTime]" +
                                            " FROM [UAUserOfRole] {0}";



        public const string UAUSEROFROLE_INSERT = " INSERT INTO [UAUserOfRole] (" +
                                            "[UserID],[RoleID],[Status],[CreateUser],[CreateTime],[LastModifyUser],[LastModifyTime]" +
                                        ") VALUES (" +
                                            "@UserID,@RoleID,@Status,@CreateUser,@CreateTime,@LastModifyUser,@LastModifyTime" +
                                        ");";


        public const string UAUSEROFROLE_UPDATE = " UPDATE [UAUserOfRole] SET " +
                                            "[RoleID]=@RoleID,[Status]=@Status,[CreateUser]=@CreateUser,[CreateTime]=@CreateTime,[LastModifyUser]=@LastModifyUser,[LastModifyTime]=@LastModifyTime" +
                                        " WHERE " +
                                            " [UserID] = @UserID";



        public const string UAUSEROFROLE_DELETE = "DELETE FROM [UAUserOfRole]" +
                                        " WHERE " +
                                            " [UserID] = @UserID";

        /// <summary>
        /// 根据UserID获取指定的UAUserOfRole
        /// </summary>
        /// <param name="UserID">UAUserOfRole主键</param>
        /// <returns></returns>
        public static UAUserOfRoleModel GetInfo(Int32 UserID)
        {

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(UAUSEROFROLE_SELECT_BY_ID);
            db.AddInParameter(dbCommand, "@UserID", DbType.Int32, UserID);

            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                if (dr.Read())
                {
                    return Emit.ConvertTo<UAUserOfRoleModel>(dr);
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 添加新的UAUserOfRole
        /// </summary>
        /// <param name="uAUserOfRole">UAUserOfRole实体</param>
        /// <returns></returns>
        public static int Add(UAUserOfRoleModel uAUserOfRole)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(UAUSEROFROLE_INSERT);

            db.AddInParameter(dbCommand, "@UserID", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.UserID));
            db.AddInParameter(dbCommand, "@RoleID", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.RoleID));
            db.AddInParameter(dbCommand, "@Status", DbType.Int16, DBConvert.ToDBValue(uAUserOfRole.Status));
            db.AddInParameter(dbCommand, "@CreateUser", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.CreateUser));
            db.AddInParameter(dbCommand, "@CreateTime", DbType.DateTime, DBConvert.ToDBValue(uAUserOfRole.CreateTime));
            db.AddInParameter(dbCommand, "@LastModifyUser", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.LastModifyUser));
            db.AddInParameter(dbCommand, "@LastModifyTime", DbType.DateTime, DBConvert.ToDBValue(uAUserOfRole.LastModifyTime));


            int count = db.ExecuteNonQuery(dbCommand);

            if (count > 0)
                return (int)dbCommand.Parameters["@UserID"].Value;
            return -1;
        }

        /// <summary>
        /// 修改WKF_WorkItemOfKit
        /// </summary>
        /// <param name="uAUserOfRole">UAUserOfRole实体</param>
        /// <returns></returns>
        public static bool Update(UAUserOfRoleModel uAUserOfRole)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(UAUSEROFROLE_UPDATE);

            db.AddInParameter(dbCommand, "@UserID", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.UserID));
            db.AddInParameter(dbCommand, "@RoleID", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.RoleID));
            db.AddInParameter(dbCommand, "@Status", DbType.Int16, DBConvert.ToDBValue(uAUserOfRole.Status));
            db.AddInParameter(dbCommand, "@CreateUser", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.CreateUser));
            db.AddInParameter(dbCommand, "@CreateTime", DbType.DateTime, DBConvert.ToDBValue(uAUserOfRole.CreateTime));
            db.AddInParameter(dbCommand, "@LastModifyUser", DbType.Int32, DBConvert.ToDBValue(uAUserOfRole.LastModifyUser));
            db.AddInParameter(dbCommand, "@LastModifyTime", DbType.DateTime, DBConvert.ToDBValue(uAUserOfRole.LastModifyTime));


            return db.ExecuteNonQuery(dbCommand) > 0;
        }

        /// <summary>
        /// 删除WKF_WorkItemOfKit 
        /// </summary>
        /// <param name="UserID">UAUserOfRole主键</param>
        /// <returns></returns>
        public static bool Delete(Int32 UserID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(UAUSEROFROLE_DELETE);
            db.AddInParameter(dbCommand, "@UserID", DbType.Int32, UserID);
            return db.ExecuteNonQuery(dbCommand) > 0;
        }
    }
}
